package cn.zbmao.web.controller;

import cn.zbmao.web.bean.Constants;
import cn.zbmao.web.bean.User;
import cn.zbmao.web.factory.ServicesFactory;
import cn.zbmao.web.services.HomeServices;
import cn.zbmao.web.util.DateUtil;
import cn.zbmao.web.util.StringOperateUtil;
import cn.zbmao.web.util.dao.DaoFactory;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.JsonRender;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;

import java.util.List;
import java.util.Map;

public class HomeController extends Controller {
//	private static final String def = "timelong";
	private static final String startime = "stime" ;
	private static final String endtime = "etime" ;
	public void index() {
        setAttr("title","我的招标猫");
        setAttr("param","user-setting");
        home();
	}


	private void home() {
//获取参数
		User user = (User) getSessionAttr("user");
		String user_district = HomeServices.getUser_district(user.getInt("id"));



		String user_keyword = HomeServices.getUser_keyword(user.getInt("id"));
		//List<Record> listUrl = HomeServices.getUser_url(user.getInt("id"));

//		List<Record> listKeyWord = DaoFactory.getUserDao().getUser_key(
//				user.getInt("id"));
        User users = DaoFactory.getUserDao().getUserById(user.getInt("id"));
        int order=0;
        if(getParaToInt("order")!=null){
            order=getParaToInt("order");
        }
        List<Map<String, Object>> successfulList = ServicesFactory.getSuccessfulServices().getSuccessfulForUser(user.getInt("id"),10,order);

        //将用户设置的属性发送到页面
        setAttr("successfulList",successfulList);
//		setAttr("user_lastlogin_time",
//				DateUtil.getDateToStr(user.getTimestamp("user_lastlogin_time")));
//		setAttr("time_last_login",
//				DateUtil.getDateToInt(user.getTimestamp("user_lastlogin_time")));
		setAttr("user_district", user_district);
		setAttr("user_keyword",  user_keyword.replaceAll(" ", ",") );
//		setAttr("urlList", listUrl);
        setAttr("website",users.get("website")==null?"":users.get("website"));
        setAttr("phone",users.get("phone")==null?"":users.get("phone"));
        setAttr("user_email",users.get("user_email"));
		setAttr("account_is_pay", user.getInt("account_is_pay")==0?"体验用户":"付费用户");
		
		setAttr("account_endtime", DateUtil.getDateToStr("yyyy-MM-dd", user.getTimestamp("account_matured_time")));


        //地区设置
        List<Record> list = DaoFactory.getUserDao().getUser_district(user.getInt("id"));
        List<Record>url_list = DaoFactory.getUserDao().getUser_district_un(user.getInt("id"));
        for (Record record:list){
            if(record.getInt("loc_id")== Constants.LOC_ID_CITY){
                Record r=DaoFactory.getUserDao().getPidById(record.getInt("district_id"));

                record.set("name_d",r.getStr("name")+"  "+record.getStr("name"));
            }else{
                record.set("name_d",record.getStr("name"));
            }

        }

        setAttr("user_distruct_list", list);
        setAttr("district_list", url_list);
//        setAttr("dl",0);
//        if(list.size()>0){
//            Record record=DaoFactory.getUserDao().isCity(user.getInt("id"));
//
//            if(record==null){
//                List<Record>url_list = DaoFactory.getUserDao().getUser_district_un(user.getInt("id"));
//                setAttr("district_list_province", url_list);
//                setAttr("dl",1);
//            }else{
//                List<Record>url_list = DaoFactory.getUserDao().getUser_district_un1(user.getInt("id"),record.getInt("pid"));
//                setAttr("district_list_city", url_list);
//                setAttr("dl",2);
//            }
//        }else{


        //关键字设置
        List<Record>keyword_list = DaoFactory.getUserDao().getUser_key(user.getInt("id"));
        setAttr("keyword_list", keyword_list);
		
        //资质设置
        List<Record> qualificationList= DaoFactory.getUserDao().findUserQualificationNameAll(user.getInt("id"));
        List<Record> unQualificationList= DaoFactory.getUserDao().UnFindUserQualificationNameAll(user.getInt("id"));
        setAttr("qualificationList", qualificationList);
        setAttr("unQualificationList", unQualificationList);
		render("/views/account/setting/profile.html");
	}
//    地域下拉选
    public void selectChange(){
        int id=getParaToInt("id");
        User user = (User) getSessionAttr("user");
        Record r=DaoFactory.getUserDao().getDisByCode(id);
        List<Record> list1=DaoFactory.getUserDao().getUser_district_sel(user.getInt("id"),r.getInt("id"));

        setAttr("list1",list1);
        render(new JsonRender().forIE());
    }

    public void sel(){
        int id=getParaToInt("id");
        User user = (User) getSessionAttr("user");
        List<Record> list=DaoFactory.getUserDao().getUser_district_sel(user.getInt("id"),id);
        if(list.size()==0){
            setAttr("msg",0);
//            Constants.cipr="province";

        }else{
            setAttr("msg", 1);
//            Constants.cipr="city";
        }
        render(new JsonRender().forIE());
    }

    /**
     * 修改密码
     */
    public void changePassword(){

        String old_password = getPara("old_password");
        String password = getPara("password");
        String re_password = getPara("re_password");
//        对输入框输入的数据经行字符串过滤处理
        old_password= Jsoup.clean(StringOperateUtil.exChange(old_password), Whitelist.basic());
        password= Jsoup.clean(StringOperateUtil.exChange(password), Whitelist.basic());
        re_password= Jsoup.clean(StringOperateUtil.exChange(re_password), Whitelist.basic());

        if(!password.equals(re_password)){
            setAttr("msg",2);
        }else{
            User user = (User) getSessionAttr("user");
            String bl = DaoFactory.getUserDao().updatePassword(user.getInt("id"),password,old_password);
            if (!StringUtils.isEmpty(bl)){
                setAttr("msg",1);
            }else{
                setAttr("msg",0);
            }
        }
        render(new JsonRender().forIE());
    }



}
